package cn.iocoder.yudao.framework.operatelog.core.Logger;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Acer
 * @auth zh 2021/11/16 14:05
 */
@Component
public class LoggerInvocationHandler implements InvocationHandler {
    private LoggerInterface loggerInterfaceImpl;
    private static final LoggerInvocationHandler LOGGER_INVOCATION_HANDLER = new LoggerInvocationHandler();

    static void InitLoggerInvocationHandler() {
        LOGGER_INVOCATION_HANDLER.loggerInterfaceImpl = Logger.getLog();
    }

    private LoggerInvocationHandler() {
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        loggerPre();
        method.invoke(loggerInterfaceImpl, args);
        loggerAfter();
        return 1;
    }

    void loggerPre() {

    }

    void loggerAfter() {
        System.out.println(".....日志记录完成.....");
    }

    LoggerInterface getProxyObj() {
        InitLoggerInvocationHandler();
        return (LoggerInterface) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{LoggerInterface.class}, this);
    }

    public static void logger(Class c) throws IOException, ClassNotFoundException {
        LoggerInterface proxyObj = LOGGER_INVOCATION_HANDLER.getProxyObj();
        proxyObj.LoggerInfile(c);
    }

    public static void logger(String methodName) throws IOException, ClassNotFoundException {
        LoggerInterface proxyObj = LOGGER_INVOCATION_HANDLER.getProxyObj();
        proxyObj.LoggerInfile(methodName);
    }
}
